package com.googlecode.maratische.google.model;

import java.util.ArrayList;
import java.util.List;

import javax.swing.SwingUtilities;

import be.lechtitseb.google.reader.api.core.GoogleReader;
import be.lechtitseb.google.reader.api.model.exception.GoogleReaderException;
import be.lechtitseb.google.reader.api.model.feed.ItemDescriptor;
import be.lechtitseb.google.reader.api.util.GoogleReaderUtil;

import com.googlecode.maratische.google.BaseException;
import com.googlecode.maratische.google.DaoAction;
import com.googlecode.maratische.google.MainController;

public class CommandUpdateFeedOnline extends CommandUpdateFeedOffline {

	@Override
	public String getType() {
		return Commands.UpdateFeedOnline.toString();
	}

	@Override
	public Integer getPriority() {
		return 2;
	}

	@Override
	public boolean execute(final DaoAction daoAction) throws BaseException, GoogleReaderException  {
		String result = null;
		GoogleReader googleReader = getGoogleReader(daoAction);
		result = googleReader.getApi().getUnreadItems(getString1(), 100);
		List<ItemDescriptor> items = GoogleReaderUtil.getItemDescriptorsFromItemsXml(result);
		List<Item> itemsResult = new ArrayList<Item>();
		for (ItemDescriptor item : items) {
			itemsResult.add(new Item(item, false));
		}
		MainController.getMainController().setItemsToItemList(itemsResult);
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				MainController.getMainController().updateItemsPanel();
			}
		});
		return true;
	}

}
